﻿@page "/parammonitor"
@using IotClientApp.Shared;
@using Microsoft.AspNetCore.SignalR.Client;
@using Newtonsoft.Json;
@inject NavigationManager Navigation
@implements IAsyncDisposable
@inject HttpClient Http
<h3>参数监控</h3>
<div class="form-group">
    <label>
        DeviceId:
        <input @bind="deviceId" />
    </label>
</div>
<div class="form-group">
    <label>
        PageIndex:
        <input @bind="pageIndex" />
    </label>
    <label>
        PageSize:
        <input @bind="pageSize" />
    </label>
    <label>@pageCount</label>
</div>
<div class="form-group">
    <label>
        Message:
        @*<input @bind="messageInput" size="50" />*@
    </label>
</div>
<button @onclick="Send" disabled="@(!IsConnected)">Send</button>

<hr>
<button @onclick="GetList">GetList</button>
<br />

<ul id="messagesList">
    @foreach (var message in messages)
    {
        <li><span>@message.Name</span>&nbsp;&nbsp;<span>@message.Value</span>&nbsp;&nbsp;<span>@message.Time.ToString("yyyy-MM-dd HH:mm:ss")</span></li>
    }
</ul>
@code {
    private HubConnection? hubConnection;
    private List<RealDataModel> messages = new List<RealDataModel>();
    private string deviceId = "1";
    private int pageSize = 20;
    private int pageIndex = 1;
    private int pageCount = 0;
    protected override async Task OnInitializedAsync()
    {
        hubConnection = new HubConnectionBuilder()
            .WithUrl(Navigation.ToAbsoluteUri("/ParamMonitorHub"))
            .Build();
        hubConnection.On<RealDataModel>("ReceiveRealData", (msg) =>
        {
            if (!messages.Any(t => t.Name == msg.Name))
            {
                return;
            }
            var message = messages.FirstOrDefault(t => t.Name == msg.Name);
            if (message != null)
            {
                message.Value = msg.Value;
                message.Time = msg.Time;
            }
            StateHasChanged();
        });

        await hubConnection.StartAsync();
    }
    public bool IsConnected =>
        hubConnection?.State == HubConnectionState.Connected;
    private async Task GetList()
    {
        var response = await Http.PostAsJsonAsync<GetRealDataModel>("api/Param/GetRealData", new GetRealDataModel
            {
                DeviceId = deviceId,
                PageIndex = pageIndex,
                PageSize = pageSize
            });
        if (response.IsSuccessStatusCode)
        {
            var responseContent = await response.Content.ReadAsStringAsync();

            Console.WriteLine(responseContent);
            try
            {
                var result = JsonConvert.DeserializeObject<PageResponse<RealDataModel>>(responseContent);
                messages = result.Data;
                pageCount = result.PageCount;
                messages = messages.ToList();
                StateHasChanged();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
    private async Task Send()
    {
        if (hubConnection is not null)
        {
            await hubConnection.SendAsync("SendRealData", deviceId);
        }
    }
    public async ValueTask DisposeAsync()
    {
        if (hubConnection is not null)
        {
            await hubConnection.DisposeAsync();
        }
    }
}